﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Speech.Synthesis;

namespace TextToSpeach.Engine
{
    public class SapiEngine
    {
        private SpeechSynthesizer synth;

        public int Volume
        {
            get { return synth.Volume; }
            set { synth.Volume = value; }
        }

        public int Speed
        {
            get { return synth.Rate; }
            set { synth.Rate = value; }
        }

        public ReadOnlyCollection<InstalledVoice> GetInstalledVoices()
        {
            ReadOnlyCollection<InstalledVoice> voices = synth.GetInstalledVoices();
            if (voices.Count == 0)
            {
                throw new Exceptions.EmptyVoicesException("No Installed Voices, please install a SAPI voice before using Text-To-Speach.");
            }
            return voices;

        }

        public void StartSynthesis(string voice, string text)
        {
            if (String.IsNullOrEmpty(voice))
                throw new Exceptions.EmptyVoicesException("Cannot use a empty string as Voice Name");
            synth.SelectVoice(voice);

            synth.SpeakAsync(text);
        }
        public void StopSynthesis()
        {
            synth.SpeakAsyncCancelAll();
        }

        public SapiEngine()
        {
            synth = new SpeechSynthesizer();
        }
    }
}
